<?php
namespace Kuoqi\Html\Template;
/**
 * 针对数据统计专门定制的模型
 * Class TotalDataer
 * @package Kuoqi\Html\Template
 * User kuoqi
 * Datetime 2022/4/2 13:46
 * Remark
 */
class TotalDataer{

    /**
     * 数据输入初始化序列
     * @var array
     * User kuoqi
     * Datetime 2022/3/21 15:13
     * Remark 数据列
     */
    public $input_data = array();
    /**
     * X坐标的时间搓字段
     * @var string
     * User kuoqi
     * Datetime 2022/4/2 13:56
     * Remark
     */
    protected $x_axis_col = '';
    /**
     * 设置统计尺度开始时间戳
     * @var int
     * User kuoqi
     * Datetime 2022/4/2 14:05
     * Remark
     */
    protected $timestamp_start = -1;
    /**
     * 设置统计时间结束时间戳
     * @var int
     * User kuoqi
     * Datetime 2022/4/2 14:05
     * Remark
     */
    protected $timestamp_end = -1;
    /**
     * 设置时间尺度上面的分割类型，支持 y,m,d,h,i
     * @var string
     * User kuoqi
     * Datetime 2022/4/2 14:07
     * Remark
     */
    protected $time_format = 'd';
    /**
     * 传统x轴配置完成之后显示的内容
     * @var array
     * User kuoqi
     * Datetime 2022/4/2 14:22
     * Remark
     */
    protected $x_axis = array();

    /**
     * 初始化
     * Tree constructor.
     *
     */
    public function __construct($data = array()){
        $this->input_data = $data;
    }

    /**
     * 配置关联时间字段
     * @param $cols
     * User kuoqi
     * Datetime 2022/4/2 13:55
     * Remark
     */
    public function setTimecols($cols){
        $this->x_axis_col = $cols;
    }

    /**
     * 设置起始的时间戳值
     * User kuoqi
     * Datetime 2022/4/2 13:57
     * Remark
     */
    public function setStartTimestamp($stimestamp = -1){
        $this->timestamp_start = $stimestamp;
    }

    /**
     * 设置结束的时间戳信息
     * @param int $etimetamp
     * User kuoqi
     * Datetime 2022/4/2 14:04
     * Remark
     */
    public function setEndTimestamp($etimestamp = -1){
        $this->timestamp_end = $etimestamp;
    }

    /**
     * 设置时间尺度上面的分割类型，支持 y,m,d,h,i
     * @param string $rimeFormat
     * User kuoqi
     * Datetime 2022/4/2 14:02
     * Remark
     */
    public function setTimeformat($timeFormat = ''){
        $this->time_format = $timeFormat;
    }

    /**
     * 初始化当前数据范围 - 获取数据中最小和最大
     * User kuoqi
     * Datetime 2022/4/2 14:08
     * Remark
     */
    private function initTimeRange(){
        foreach($this->input_data as $item){
            if($this->timestamp_start == -1){ $this->timestamp_start = $item[$this->x_axis_col];}
            if($this->timestamp_end == -1){ $this->timestamp_end = $item[$this->x_axis_col];}
            $this->timestamp_start = min($this->timestamp_start,$item[$this->x_axis_col]);
            $this->timestamp_end = max($this->timestamp_end,$item[$this->x_axis_col]);
        }
    }

    /**
     * 初始化数据坐标信息
     * User kuoqi
     * Datetime 2022/4/2 14:14
     * Remark
     */
    public function initTimeLimit(){
        switch($this->time_format){
            default:$this->initTimeLimitByDate();break;//根据日期初始化坐标节点
        }
    }

    /**
     * 根据日期初始化坐标借点
     * User kuoqi
     * Datetime 2022/4/2 14:16
     * Remark
     */
    public function initTimeLimitByDate(){
        $start = strtotime(date('Y-m-d',$this->timestamp_start));
        $list = array();
        for($i = $start;$i<$this->timestamp_end;$i+=86400){
            $list[] = array(
                'timestamp_start'=>$i,
                'timestamp_end'=>$i+86400,
                'simple_code'=>date('Y-m-d',$i),
            );
        }
        $this->x_axis = $list;
    }

    /**
     * 根据时间段获取数据量
     * @param $stimestamp
     * @param $etimestamp
     * @return int
     * User kuoqi
     * Datetime 2022/4/2 14:26
     * Remark
     */
    private function findCountByTimelen($stimestamp,$etimestamp){
        $count = 0;
        foreach($this->input_data as $item){
            if($item[$this->x_axis_col]>=$stimestamp && $item[$this->x_axis_col] < $etimestamp){
                $count ++;
            }
        }
        return $count;
    }

    /**
     * 写入数据对应的信息
     * User kuoqi
     * Datetime 2022/4/2 14:23
     * Remark
     */
    public function writeTimedata(){
        foreach ($this->x_axis as $i=>$it){
            $this->x_axis[$i]['data'] = $this->findCountByTimelen($it['timestamp_start'],$it['timestamp_end']);
        }
    }

    /**
     * 以线性的方式输出统计信息
     * User kuoqi
     * Datetime 2022/4/2 14:07
     * Remark
     */
    public function totalWithLine(){
        $this->initTimeRange();//初始化数据范围
        $this->initTimeLimit();//初始化数据坐标信息
        $this->writeTimedata();//写入数据对应的信息
        return $this->x_axis;
    }

}